package textBuddyProcessor;

import java.io.IOException;
import java.util.Scanner;

/*
 * ==============NOTE TO USER==========================================
 * This class basically contains the main function and is responsible for 
 * calling the other methods, to carry on input, processing and output.
 * ====================================================================
 */

public class TextBuddy {

	private static final String MESSAGE_WELCOME = "Welcome to Text Buddy, the System is Ready for use";
	private static final String MESSAGE_THANK_YOU = "Thank You for using TextBuddy";
	private static final String MESSAGE_WRONG_ARGUMENT = "There was no input"
			+ " provided or the file name was wrong";
	private static final int PARAM_FILE_NAME = 0;

	public static void main(String args[]) throws IOException {

		displayToUser(MESSAGE_WELCOME);

		Scanner s = new Scanner(System.in);
		String userFileName = checkforIncorrectArguments(args, s);

		ControllerLogic beginProcessing = new ControllerLogic();
		InputProcessor inputProcessor = new InputProcessor();

		inputProcessor.setEnvironment(userFileName, beginProcessing);
		inputProcessor.printReadyMessage();

		beginProcessing.executeCommandsUntilExitCommand(s);
		displayToUser(MESSAGE_THANK_YOU);
	}

	private static void displayToUser(String welcomeMessage) {
		System.out.println(welcomeMessage);

	}

	public static String checkforIncorrectArguments(String[] args, Scanner s) {

		String userCommand;
		InputValidator input = new InputValidator();

		boolean isNoArguments = input.checkIfNoArguments(args, s);
		if (isNoArguments) {
			System.out.println(MESSAGE_WRONG_ARGUMENT);
			userCommand = input.askToReenterData(s);
		} else {
			userCommand = args[PARAM_FILE_NAME];
		}

		boolean isWrongfileName = input.checkIfWrongFileName(userCommand, s);

		while (isWrongfileName) {
			userCommand = input.askToReenterData(s);
			isWrongfileName = input.checkIfWrongFileName(userCommand, s);
		}
		return userCommand;
	}
}
